/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.io.File;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import stamboom.*;

/**
 *
 * @author Cabez
 */
@SuppressWarnings("serial")
public class StamboomFrame extends javax.swing.JFrame {

    /**
     * Creates new form StamboomFrame
     */
    private Administratie admin;

    @SuppressWarnings("unchecked")
    public StamboomFrame(Administratie adm) {
        initComponents();
        this.admin = adm;
        jComboBox1.removeAllItems();
        Iterator it = admin.getAllepersonen();
        if (it != null){
            while (it.hasNext()){
                jComboBox1.addItem(it.next().toString());
            }
        }
        
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jbtnToonPersoon = new javax.swing.JButton();
        dumpButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jmiOpslaanAls = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jmiPersoon = new javax.swing.JMenuItem();
        jmiGezin = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Personen");

        jbtnToonPersoon.setText("Toon Persoon");
        jbtnToonPersoon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnToonPersoonActionPerformed(evt);
            }
        });

        dumpButton.setText("Dump!");
        dumpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dumpButtonActionPerformed(evt);
            }
        });

        jMenu1.setText("Bestand");

        jmiOpslaanAls.setText("Opslaan als");
        jmiOpslaanAls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiOpslaanAlsActionPerformed(evt);
            }
        });
        jMenu1.add(jmiOpslaanAls);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Toevoegen");

        jmiPersoon.setText("Persoon");
        jmiPersoon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiPersoonActionPerformed(evt);
            }
        });
        jMenu2.add(jmiPersoon);

        jmiGezin.setText("Gezin");
        jmiGezin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiGezinActionPerformed(evt);
            }
        });
        jMenu2.add(jmiGezin);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jbtnToonPersoon, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(dumpButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnToonPersoon)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 108, Short.MAX_VALUE)
                .addComponent(dumpButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmiPersoonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiPersoonActionPerformed
        PersoonAanmeldDialog pad = new PersoonAanmeldDialog(new javax.swing.JFrame(), rootPaneCheckingEnabled, admin);
        pad.setVisible(true);
        jComboBox1.removeAllItems();
        for (Iterator<Persoon> it = admin.getAllepersonen(); it.hasNext();) {
        if (it != null){
            while (it.hasNext()){
                jComboBox1.addItem(it.next().toString());
                }
            }
        }
        
        //this.dispose();
        
    }//GEN-LAST:event_jmiPersoonActionPerformed

    private void jmiOpslaanAlsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiOpslaanAlsActionPerformed

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("./"));
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int result = chooser.showSaveDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File fileName;
            fileName = new File(chooser.getSelectedFile()+".output");
           
            if (fileName.exists()){
                result= JOptionPane.showConfirmDialog(this, "Replace existing file?");
                
                if(result == JOptionPane.NO_OPTION){
                    return;
                }
            }
            admin.save(fileName.getPath());
        }

    }//GEN-LAST:event_jmiOpslaanAlsActionPerformed

    private void jmiGezinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiGezinActionPerformed
        GezinFrame frame = new GezinFrame(admin);
        frame.setVisible(true);
        //this.dispose();
        
    }//GEN-LAST:event_jmiGezinActionPerformed

    private void jbtnToonPersoonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnToonPersoonActionPerformed
     
        Persoon p = null;
        int selectedIndexGezin= jComboBox1.getSelectedIndex();
        if(selectedIndexGezin >= 0){
            int t= 0;
            Iterator<Persoon> persons= admin.getAllepersonen();
            while(persons.hasNext()){
                if(t == selectedIndexGezin){
                    p = persons.next();
                    break;
                }else{
                    persons.next();
                }
                t++;
            }
            BestaandPersoonFrame bpf = new BestaandPersoonFrame(p);
            bpf.setVisible(true);
        }


        
    }//GEN-LAST:event_jbtnToonPersoonActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void dumpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dumpButtonActionPerformed
        admin.saveDB();
    }//GEN-LAST:event_dumpButtonActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton dumpButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JButton jbtnToonPersoon;
    private javax.swing.JMenuItem jmiGezin;
    private javax.swing.JMenuItem jmiOpslaanAls;
    private javax.swing.JMenuItem jmiPersoon;
    // End of variables declaration//GEN-END:variables
}
